package org.dei.perla.aggregates;

import java.math.BigDecimal;

import java.lang.Object;

import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Random;

import org.dei.perla.parser.expressions.Constant;
import org.dei.perla.parser.expressions.ConstantCastException;
import org.dei.perla.parser.expressions.ConstantFloat;
import org.dei.perla.parser.expressions.ConstantID;
import org.dei.perla.parser.expressions.ConstantInteger;
import org.dei.perla.parser.expressions.ConstantTimestamp;

import org.dei.perla.sys.query.llqruntime.datastructures.QueryFieldStructure;
import org.dei.perla.sys.query.llqruntime.datastructures.QueryRecordStructure;
import org.dei.perla.sys.query.llqruntime.datastructures.Record;

public class ThreadWriteBuffer implements Runnable {
	private boolean flag = true;
	private Buffer pBuffer;
	int i=0;
	// private QueryRecordStructure qrs;
	public ThreadWriteBuffer(Buffer pBuffer) {
		this.pBuffer = pBuffer;
	}
	public void run() { // Generating a Buffer sample with 20 records.
		QueryRecordStructure qrs = initQRS();
		while(i<20){
			Record record = randomRecord(qrs);
			if (pBuffer.addRecord(record)){
				Timestamp tt = null;
				try {
					tt = new Timestamp(record.getField("timestamp").getValueTimestamp());
				} catch (ConstantCastException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			//	ConstantTimestamp cts = record.getTimestamp();
				int tIndexEnd = pBuffer.getIndexByTimestamp(record.getTimestamp());
				System.err.println("New record Added Index: " + tIndexEnd +" ID "+ record.getField("id") + " - TS: "
					+ tt + " - TEMP : " + record.getField("temp"));
			}else
				System.err.println("Error adding records to the local buffer");
			try {
				Thread.sleep((long) (2000));
			} catch (InterruptedException e) {
				e.printStackTrace();
			}	
			i++;
		}
		System.err.println("Finish adding record...a last record is added as flag");
		pBuffer.addRecord(null);
	}
//	Random sampling generation
	public static Record randomRecord(QueryRecordStructure qrs) {
		//initQRS
		Record rec = new Record(qrs);
		Constant c1 = new ConstantID("A");
//		Class cc1 = Class.forName("ConstantID");
//		Constructor ct = cc1.getConstructor(partypes);
		Date now = new Date();
	    long nowLong = now.getTime();
	//	Constant c2 = new ConstantTimestamp(nowLong);
		Constant c2 = new ConstantTimestamp();
		Constant c3 = new ConstantFloat(BigDecimal.valueOf(
				Math.random() * 10).intValue());
		rec.setField("id", c1);
		rec.setField("timestamp", c2);
		rec.setField("temp", c3);
		return rec;
	}
	/*
	 * Defining the structure
	 */
	public static QueryRecordStructure initQRS(){
		QueryRecordStructure qrs = new QueryRecordStructure();
		qrs.addFieldStructure("id", ConstantID.class);
		qrs.addFieldStructure("timestamp", ConstantTimestamp.class);
		qrs.addFieldStructure("temp", ConstantFloat.class);
		return qrs;
	}

	public static void printBufferContent(Buffer pBuffer) throws ConstantCastException {
		ArrayList<Record> list = pBuffer.getBuffer();
		Iterator<Record> it = list.iterator();
		while (it.hasNext()) {
			Record t = it.next();
			if (t!=null){
				System.err.println("idN: " + t.getField("id") + " - TS: "
					+ t.getField("timestamp") + " - TEMPERATURA : " + t.getField("temp"));
			}
		}
	}
}


